
window.signStore = {
    // 1. 考虑到切换用户，还是不要用一个变量来存了。这点性能，不值得
    // 2. 存储的是签到成功的日期，故可信（在 check或signin接口返回成功时设置)
    storeToday: function() {
        $apix.setStorage(this._key(), this._todayDate());
    },

    isSignedToday: function() {
        var date = $apix.getStorage(this._key());
        return (str_being(date) && date == this._todayDate());
    },
    foo: function() {
        tlog('today is: ' + this._todayDate());
    },

    // for debug
    _clear: function() {
        $apix.rmStorage(this._key());
    },
    _todayDate: function() {
        var d = new Date();
        return (d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate());
    },
    _key: function() {
        var myId = app.auth.getMyId();
        return (myId + '-last-signin-date');
    }
}

function renderSignIn() {
    /*
    {
        "status": "200",
        "msg": "",
        "data": {
            "signInStatus": 0
        }
    }
    */

    // for debug
    // window.signStore._clear();
    // window.signStore.foo();

    if (window.signStore.isSignedToday()) {
        setSignInUI(1);
    } else {
        fajax_get(cfg.signin_api, {action: 'check'}, {
            succ: function(json) {
                var done = json.data.signInStatus;
                setSignInUI(done);
                if (done) {
                    window.signStore.storeToday();
                }
            },
            fail: function(json, url) {
                utils.postErrNoTips(json, url);
            }
        });
    }
}

function setSignInUI(done) {
    var sign = $api.byId('signin');
    $api.attr(sign, 'data-signed', done ? 1 : 0);

    var tips = $api.byId('signin-tips');
    tips.innerHTML = done ? '今日已签到' : '签到领金币';
}

function trySignIn(bPrettyToast) {
    // test 请保留，以调试。谢谢
    // views.openToplineWin({
    //     tidHot: 19849,
    //     notDelay: true
    // });
    // return;
    // onLoginDo(function() {
    //     if (!window.signStore.isSignedToday()) {
    //         doSignIn(bPrettyToast);
    //     } else {
    //         views.openSignInWin();
    //     }
    // });
}

var isMyCenterSigning = false;
function doSignIn(bPrettyToast, isInSign) {
    if (isMyCenterSigning) {
        return;
    }

    isMyCenterSigning = true;

    utils.loading();
    fajax_get(cfg.signin_api, {action: 'signin'}, {
        always: function() {
            utils.rmLoading();
            isMyCenterSigning = false;
        },
        succ: function(json) {
            window.signStore.storeToday();

            if (isInSign) {
                updateSignUI && updateSignUI();
            }else {
                setSignInUI(1);
            }
            evt.send('update-credits', {});

            var ms = 3000;
            if (bPrettyToast) {
                toastSucc(json.msg, ms);
            } else {
                utils.toast(json.msg, ms);
            }

            if (!isInSign) {
                setTimeout(function(){
                    views.openSignInWin();
                }, ms)
            }
        },
        fail: function(json, url) {
            utils.longToast(json.msg);
            utils.postErrNoTips(json, url);
        },
    });
}

function toastSucc(msg, dur) {
    var toast = new auiToast({});
    toast.custom({
        title: msg,
        // html: '<i class="aui-iconfont aui-icon-laud"></i>',
        html: '<img class="toast-coin" src="../../../global/image/coin-star.png" />',
        duration: dur || 3000
    });
}

function fooToastSucc() {
    var msg = '恭喜您签到成功，奖励金币3个';
    toastSucc(msg, 200000);
}
